package core;

import static util.ConstantCollection.*;
import java.io.File;
import util.FileWork;
import util.TestConfiguration;
import static core.SeleneseTestCaseEx.repEng;
import static core.SeleneseTestCaseEx.*;

public class HandlingError {
	
	private static final HandlingError INSTANCE = new HandlingError();
	private static TestConfiguration tc = TestConfiguration.getInstance();
	private FileWork fw = new FileWork();
	private Integer scrNum = 0;
	private Object[] errorDesc = new Object[3];
	
	
	public static HandlingError getInstance() {
	      return INSTANCE;
	}
	
	
	
	private Integer nextScrNum() {
		scrNum = scrNum + 1; 
		return scrNum;
	}
	
	
	public void setErrDesc(Exception e) {
		errorDesc[0] = "error";
		errorDesc[1] = e.getMessage();
		errorDesc[2] = e.getStackTrace(); 
	}
	
	public void setFailDesc(String description) {
		errorDesc[0] = "fail";
		errorDesc[1] = description;
		errorDesc[2] = null;
	}
	
	public void setWarnDesc(String description) {
		errorDesc[0] = "warning";
		errorDesc[1] = description;
		errorDesc[2] = null;
	}
	
	public void checkResult() {
		Integer status = STATUS_UNCHECKED;
		
		if (errorDesc[0] == null) {
			status = STATUS_PASS;
			repEng.addResult(status, "", null, null);
		} else {	
			if (((String) errorDesc[0]).contentEquals("error")) {
				status = STATUS_ERROR;
			} else if (((String) errorDesc[0]).contentEquals("fail")) {
				status = STATUS_FAIL;
			} else if (((String) errorDesc[0]).contentEquals("warning")) {
				status = STATUS_WARNING;
			}
			String pathToScrShot = null;
			
			if (tc.getEnv("getscrshot").toLowerCase().contentEquals("true")) {
				try {
					String absolutePath;
					pathToScrShot = repEng.getStepId() + "_" + nextScrNum().toString() + ".png";
					
					absolutePath = u.getFullPathToLog("datetime") + "\\"+ "source\\scrsht\\" + pathToScrShot; 
				
					pathToScrShot = (captureScreenshot(absolutePath) == null) ? null : pathToScrShot;
				} catch (Exception e) {
					System.err.println(e.getMessage());
					e = null;
				}
			}
			
			
			repEng.addResult(status, (String) errorDesc[1], (StackTraceElement[]) errorDesc[2], pathToScrShot);
			
		}
		

		errorDesc[0] = null;
		errorDesc[1] = null;
		errorDesc[2] = null;
	}
	
	public String captureScreenshot(String pathToScrShot) {
		String browserName = SeleneseTestCaseEx.getBrowser().toLowerCase();
		try {
			if (SeleneseTestCaseEx.getServerName().toLowerCase().contentEquals("localhost")) {
	
				if (browserName.contains("firefox") || browserName.contains("chrome")) { //only for firefox browser 
					browser.captureEntirePageScreenshot(pathToScrShot, "");
				} else {
					browser.captureScreenshot(pathToScrShot);
				}
			} else {
				if (browserName.contains("firefox") || browserName.contains("chrome")) { //only for firefox browser
					fw.saveBase64ToPNG(pathToScrShot, browser.captureEntirePageScreenshotToString(""));
				} else {
					fw.saveBase64ToPNG(pathToScrShot, browser.captureScreenshotToString());
				}
			}
		} catch (Exception e) {
			return null;
		}
		
		return pathToScrShot;
		
	}

}

